package com.ztw.yygh.user.api;

import com.ztw.yygh.common.result.Result;
import com.ztw.yygh.common.utils.AuthContextHolder;
import com.ztw.yygh.model.user.UserInfo;
import com.ztw.yygh.user.service.UserInfoService;
import com.ztw.yygh.vo.user.LoginVo;
import com.ztw.yygh.vo.user.UserAuthVo;
import org.apache.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Resource
    private UserInfoService userInfoService;

    //用户手机号登录接口
    @PostMapping("login")
    public Result login(@RequestBody LoginVo login) {
        Map<String, Object> info = userInfoService.loginUser(login);
        return Result.ok(info);
    }

    //用户认证接口
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //两个参数 id userAuthVo
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    //获取用户id信息接口
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId=AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

}
